package com.zhongxi.guoyuan.view.personalCenter;

import android.os.Build;
import android.view.View;
import android.webkit.WebView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.base.BaseActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class DynamicsDetailActivity extends BaseActivity implements View.OnClickListener {

    private String html;
    private WebView webView;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_dynamics_detail);
        initTitle();
        tvTitle.setText("活动详情");
        webView = (WebView) findViewById(R.id.webView);
        html = getIntent().getStringExtra("html");
    }

    @Override
    protected void initData() {
        super.initData();
        initWebView();
        if(null!=html&&!"".equals(html)){
            webView.loadDataWithBaseURL(null, getNewContent(html), "text/html", "utf-8", null);
        }
    }

    private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        return doc.toString();
    }

    private void initWebView() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.getSettings().setBlockNetworkImage(false);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            webView.getSettings().setMixedContentMode(webView.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
